package com.pc.controller;

import com.pc.enrty.Food;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInfo;
import com.pc.enrty.Food;
import com.pc.enrty.ShoppingTrolley;
import com.pc.mapper.FoodMapper;
import com.pc.pojo.Cart;
import com.pc.service.FoodService;
import com.pc.service.ShoppingTrolleyService;
import com.pc.service.impl.FoodServiceImpl;
import com.pc.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pc/trolley/")
public class ShoppingTrolleyController {
    @Autowired
    private ShoppingTrolleyService shoppingTrolleyService;
    @Autowired
    private ResultObject result;
    @Autowired
    private FoodService foodService;
@RequestMapping("findFid")
    public ResultObject getResult(ShoppingTrolley shoppingTrolley) {
    ArrayList<Cart> list = shoppingTrolleyService.findll(shoppingTrolley);
    result.setResult(list);
    return result;
    }

    @RequestMapping("find")
    public ResultObject findByUidAndFid(Cart cart){
        ArrayList<Cart> list = shoppingTrolleyService.findByUidAndFid(cart);
        result.setResult(list);
        return result;
    }
    @RequestMapping("add")
    public ResultObject addTrolley(ShoppingTrolley shoppingTrolley,Food food){
        PageInfo<Food> food1 = foodService.findByIdCidAndName(food, 0, 0);
        Food f1 = new Food();
        for (Food f : food1.getList()) {
            f1.setId(f.getId());
            f1.setPrice(f.getPrice());
        }
        shoppingTrolley.setFid(f1.getId());
        Integer quantity = shoppingTrolley.getQuantity();
        Double price = f1.getPrice();
        double v = quantity * price;
        shoppingTrolley.setAmount(v);
        shoppingTrolley.setUid(1);
        boolean i = shoppingTrolleyService.addTrolley(shoppingTrolley);
        result.setResult(i);
        return result;
    }
    @RequestMapping("update")
    public ResultObject updateByUidAndFid(ShoppingTrolley shoppingTrolley,Cart cart){
        ArrayList<Cart> list = shoppingTrolleyService.findByUidAndFid2(shoppingTrolley);
        System.out.println();
        Integer quantity = shoppingTrolley.getQuantity();
        double v=0.0;
        for (Cart c:list
        ) {
            v=quantity*c.getPrice();
            System.out.println(v);
        }
        shoppingTrolley.setAmount(v);
        System.out.println(shoppingTrolley);
        boolean i = shoppingTrolleyService.updateByUidAndFid(shoppingTrolley);
        if(i){
            result.setStatus(400);
            result.setResult("修改成功");
        }
        else {
            result.setStatus(406);
            result.setResult("修改失败");
        }
        return result;
    }
    @RequestMapping("remove")
    public ResultObject removeByUidAndFid(ShoppingTrolley shoppingTrolley){
        boolean i = shoppingTrolleyService.removeByUidAndFid(shoppingTrolley);

        if(i){
            result.setStatus(400);
            result.setResult("删除成功");
        }
        else {
            result.setStatus(406);
            result.setResult("删除失败");
        }
        return result;
    }
}
